package com.mkh.st.admin.strategy;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mkh.st.admin.api.entity.SysCompanyInfo;
import com.mkh.st.admin.api.entity.SysUserRoleScope;
import com.mkh.st.admin.api.vo.DataPermissionVO;
import com.mkh.st.admin.api.vo.ScopeDataDeatil;
import com.mkh.st.admin.api.vo.ScopeDataVO;
import com.mkh.st.admin.api.vo.UserRoleScopeVO;
import com.mkh.st.admin.mapper.SysCompanyInfoMapper;
import com.mkh.st.common.core.constant.CommonConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class CompanyStrategy extends BaseScopeStrategy {

	@Autowired
	private SysCompanyInfoMapper sysCompanyInfoMapper;

	private final List<Long> ids= Arrays.asList(-1L);


	@Override
	public void setDataPermission(DataPermissionVO vo, SysUserRoleScope sysUserRoleScope, Long tenantId) {
		if(ObjectUtil.isNotNull(sysUserRoleScope)){
			if(CommonConstants.ONE.equals(sysUserRoleScope.getAllFlag())){
				vo.setCompanyAll(Boolean.TRUE);
				List<SysCompanyInfo> list = sysCompanyInfoMapper.selectList(Wrappers.<SysCompanyInfo>lambdaQuery()
					.eq(SysCompanyInfo::getDelFlag,CommonConstants.NOT_DEL).eq(SysCompanyInfo::getStatus,CommonConstants.ONE)
					.eq(SysCompanyInfo::getTenantId,tenantId).orderByDesc(SysCompanyInfo::getCompanyId));
				if(CollectionUtil.isNotEmpty(list)){
					List<Long> companyIds=list.stream().map(SysCompanyInfo::getCompanyId).collect(Collectors.toList());
					vo.setCompanyList(companyIds);
				}
			}else {
				if(StrUtil.isNotBlank(sysUserRoleScope.getIds())){
					List<Long> allCompanyIds=Convert.toList(Long.class, StrUtil.split(sysUserRoleScope.getIds(),','));
					//未删除未禁用查询
					List<SysCompanyInfo> list = sysCompanyInfoMapper.selectList(Wrappers.<SysCompanyInfo>lambdaQuery()
						.eq(SysCompanyInfo::getDelFlag,CommonConstants.NOT_DEL).eq(SysCompanyInfo::getStatus,CommonConstants.ONE)
						.in(SysCompanyInfo::getCompanyId,allCompanyIds).orderByDesc(SysCompanyInfo::getCompanyId));
					if(CollectionUtil.isNotEmpty(list)){
						List<Long> companyIds=list.stream().map(SysCompanyInfo::getCompanyId).collect(Collectors.toList());
						vo.setCompanyList(companyIds);
					}else {
						vo.setCompanyList(ids);
					}
				}else {
					vo.setCompanyList(ids);
				}
			}
		}else {
			vo.setCompanyList(ids);
		}
	}

	@Override
	public Long setKey() {
		return 1L;
	}

	@Override
	public ScopeDataDeatil getScopeDataList(DataPermissionVO vo, SysUserRoleScope one,Long scopeId) {
		ScopeDataDeatil deatil=new ScopeDataDeatil();
		deatil.setAllFlag(vo.getCompanyAll());
		deatil.setScopeId(scopeId);
		List<ScopeDataVO> scopeList =new ArrayList<>();
		if(CollectionUtil.isEmpty(vo.getCompanyList())){
			return deatil;
		}
		//当前操作人的公司权限
		List<SysCompanyInfo> list = sysCompanyInfoMapper.selectBatchIds(vo.getCompanyList());
		//被操作账号已有的公司权限
		List<Long> exist=new ArrayList<>();
		boolean flag=false;
		if(ObjectUtil.isNotNull(one)){
			if(one.getAllFlag().equals(CommonConstants.ONE)){
				flag=true;
			}else if(StrUtil.isNotBlank(one.getIds())){
				exist=Convert.toList(Long.class, StrUtil.split(one.getIds(),','));
			}
		}
		if(CollectionUtil.isNotEmpty(list)){
			for (SysCompanyInfo item:list) {
				ScopeDataVO scopeDataVO=new ScopeDataVO();
				scopeDataVO.setId(item.getCompanyId()).setCode(item.getCompanyCode()).setName(item.getCompanyName());
				if(flag || exist.stream().filter(v ->v.equals(item.getCompanyId())).findFirst().isPresent()){
					scopeDataVO.setChecked(Boolean.TRUE);
				}
				scopeList.add(scopeDataVO);
			}
			deatil.setScopeList(scopeList);
		}
		return deatil;
	}

	@Override
	public void getUserDataPermission(DataPermissionVO vo, DataPermissionVO item) {
		if(CollectionUtil.isNotEmpty(item.getCompanyList())){
			if(vo.getCompanyAll()){
				return;
			}else if(item.getCompanyAll()){
				vo.setCompanyAll(item.getCompanyAll());
				vo.setCompanyList(item.getCompanyList());
			}else {
				if(CollectionUtil.isNotEmpty(vo.getCompanyList())){
					List<Long> collect = Stream.of(vo.getCompanyList(),item.getCompanyList()).flatMap(Collection::stream).distinct().collect(Collectors.toList());
					vo.setCompanyList(collect);
				}else {
					vo.setCompanyList(item.getCompanyList());
				}
			}
		}
	}

	@Override
	public List<Long> getList(Long tenantId) {
		List<SysCompanyInfo> list = sysCompanyInfoMapper.selectList(Wrappers.<SysCompanyInfo>lambdaQuery()
				.eq(SysCompanyInfo::getDelFlag,CommonConstants.NOT_DEL).eq(SysCompanyInfo::getStatus,CommonConstants.ONE)
				.eq(SysCompanyInfo::getTenantId,tenantId).orderByDesc(SysCompanyInfo::getCompanyId));
		if(CollectionUtil.isNotEmpty(list)){
			List<Long> companyIds=list.stream().map(SysCompanyInfo::getCompanyId).collect(Collectors.toList());
			return	companyIds;
		}
		return null;
	}

	@Override
	public void getAllList(UserRoleScopeVO userRoleScopeVO, SysUserRoleScope scope, Long tenantId) {
		if(CommonConstants.ZERO.equals(scope.getAllFlag())&& StrUtil.isBlank(scope.getIds())){
			return;
		}
		Boolean all=false;
		if(CommonConstants.ONE.equals(scope.getAllFlag())){
			all=true;
		}
		List<SysCompanyInfo> list = sysCompanyInfoMapper.selectList(Wrappers.<SysCompanyInfo>lambdaQuery()
				.eq(SysCompanyInfo::getDelFlag,CommonConstants.NOT_DEL)
				.eq(SysCompanyInfo::getStatus,CommonConstants.ONE)
				.eq(SysCompanyInfo::getTenantId,tenantId)
				.in(!all,SysCompanyInfo::getCompanyId,Convert.toList(Long.class, StrUtil.split(scope.getIds(),',')))
				.orderByDesc(SysCompanyInfo::getCompanyId));
		if(CollectionUtil.isNotEmpty(list)){
			List<ScopeDataVO> scopeList =new ArrayList<>();
			for (SysCompanyInfo item:list) {
				ScopeDataVO scopeDataVO=new ScopeDataVO();
				scopeDataVO.setId(item.getCompanyId()).setCode(item.getCompanyCode())
					.setName(item.getCompanyName());
				scopeList.add(scopeDataVO);
			}
			userRoleScopeVO.setScopeList(scopeList);
		}
	}

	@Override
	public Boolean checkPermission(SysUserRoleScope one, String name, Long tenantId) {
		SysCompanyInfo sysCompanyInfo = sysCompanyInfoMapper.selectOne(Wrappers.<SysCompanyInfo>lambdaQuery()
			.eq(SysCompanyInfo::getCompanyName,name).eq(SysCompanyInfo::getTenantId,tenantId)
			.eq(SysCompanyInfo::getDelFlag,CommonConstants.ZERO));
		if(ObjectUtil.isNotNull(sysCompanyInfo)&&StrUtil.isNotBlank(one.getIds())){
			List<Long> ids = Convert.toList(Long.class, StrUtil.split(one.getIds(), ','));
			if(ids.contains(sysCompanyInfo.getCompanyId())){
				return Boolean.TRUE;
			}
		}
		return Boolean.FALSE;
	}


}
